package music.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import music.entity.Manager;
import music.entity.User;
import music.util.DBUtil;
import musicdao.UserDao;

//�û�
public class UserDaoImpl implements UserDao{
	public final static UserDaoImpl DAO =  new UserDaoImpl();
	private static Logger Log = LoggerFactory.getLogger(UserDaoImpl.class);
	@Override
	//�û���¼
	public User login(String name,String password) {
		
		Connection conn = DBUtil.getConnection();
		String sql = "select * from `user` where username='"+name+"' AND `password`='"+password+"'";
		PreparedStatement ps = null;
		ResultSet count =null;	
		User user = null ;
		try {
			ps = conn.prepareStatement(sql);
			count=ps.executeQuery();
			while (count.next()) {
				user = new User();
				user.setId(count.getInt("id"));
				user.setUsername(count.getString("username"));
				user.setPassword(count.getString("password"));
				
			}
		
		} catch (Exception e) {
			Log.error("����Ա��¼���ִ���", e);
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps,null);
		}
		return user;
	}

	@Override
	//�û��˳�
	public void quit() {
		//������ҳ
		
	}
	//���id�����û�
public User search(String name) {
		
		Connection conn = DBUtil.getConnection();
		String sql = "select * from `user` where username='"+name+"'";
		PreparedStatement ps = null;
		ResultSet count =null;	
		User user = null ;
		try {
			ps = conn.prepareStatement(sql);
			count=ps.executeQuery();
			while (count.next()) {
				user = new User();
				user.setId(count.getInt("id"));
				user.setUsername(count.getString("username"));
				user.setPassword(count.getString("password"));	
			}
		
		} catch (Exception e) {
			Log.error("����Ա��¼���ִ���", e);
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps,null);
		}
		return user;
	}
public int register(String username, String password) {
	Connection conn = DBUtil.getConnection();
	String sql = "INSERT INTO user (username,password) VALUES (?,?)";
	PreparedStatement ps = null;
	int rs = 0;
	
	try {
		ps = conn.prepareStatement(sql);	
		ps.setString(1,username);
		ps.setString(2,password);
		rs = ps.executeUpdate();// 查询			
		
	} catch (Exception e) {
		Log.error("注册失败！", e);
		e.printStackTrace();
	} finally {
		DBUtil.close(conn, ps, null);
	}return rs;
	}

	

	

}
